import logging
from bumblebee.lib.base import *

log = logging.getLogger(__name__)

class DashboardController(SecureController):

    def index(self):
        c.projects = model.Project.select_by(parent_project_id=None, archived=False)
        c.archived_projects = model.Project.select_by(parent_project_id=None, archived=True)
        c.users = model.User.select()
        return render('/dashboard/index.mako')

    @validate(schema=forms.NewProjectForm(), form='index')
    def new_project(self):
        project_name = self.form_result['name']
        contact_id = self.form_result['contact_id']
        main_project = model.Project(name=project_name,
                                     description=self.form_result['description'],
                                     contact_id=contact_id)
        main_project.flush()
        if self.form_result['subprojects']:
            dev_project = model.Project(name='%s - development' % project_name,
                                        description='Development project for %s' % project_name,
                                        contact_id=contact_id,
                                        parent_project=main_project)
            doc_project = model.Project(name='%s - documentation' % project_name,
                                        description='Documentation project for %s' % project_name,
                                        contact_id=contact_id,
                                        parent_project=main_project)
            qa_project = model.Project(name='%s - qa' % project_name,
                                       description='Quality assurance project for %s' % project_name,
                                       contact_id=contact_id,
                                       parent_project=main_project)
            model.flush_all()
        redirect_to(action='index')

